﻿using BCCommon;
using BCEntity.TMS.MoveHouseConfig.MoveHouseConfig;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace BCData.TMS.MoveHouseConfig.MoveHouseConfig
{
    public class MoveHouseConfigData : IMoveHouseConfigData
    {
        private readonly string SQL_QUERY_VIEW = @" SELECT 
                                                  A.MoveHouseConfigId,
                                                  A.MoveHouseConfigName,
                                                  A.MoveHouseConfigPinYin,
                                                  A.ParentId,
                                                  A.AllParentId,
                                                  A.IsParent,
                                                  A.Level,
                                                  A.Remark,
                                                  A.Enabled,
                                                  A.PhotoAddress,
                                                  A.CreateAdminId,
                                                  A.CreateAdminName,
                                                  A.CreateTime,
                                                  A.UpdateTime,
                                                  A.EditAdminId,
                                                  A.EditAdminName
                                                FROM
                                                  movehouseconfig A ";
        private readonly string SQL_DELETE = @" DELETE FROM movehouseconfig ";
        private readonly IDatabaseContext database;
        public MoveHouseConfigData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<MoveHouseConfigEntity> InsertAsync(MoveHouseConfigEntity entity)
        {
            return await database.Master.InsertAsync(entity, database.Transaction);
        }
        public async Task<MoveHouseConfigEntity> UpdateAsync(MoveHouseConfigEntity entity)
        {
            return await database.Master.UpdateAsync(entity, database.Transaction);
        }
        public async Task<MoveHouseConfigEntity> GetSingle(long moveHouseConfigId)
        {
            var sql = typeof(MoveHouseConfigEntity).GetSelectSqlByPrimaryKeys();
            return await database.Slave.QueryFirstOrDefaultAsync<MoveHouseConfigEntity>(sql, new { moveHouseConfigId });
        }
        public async Task<IEnumerable<MoveHouseConfigEntity>> GetAllList(bool? isEnable, int level)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (isEnable.HasValue)
            {
                wheres.Add("A.Enabled = ?Enabled");
                parameters.Add("?Enabled", isEnable.Value, DbType.Boolean);
            }
            wheres.Add("A.ParentId = 0");
            wheres.Add("A.Level = ?Level");
            parameters.Add("?Level", level, DbType.Int64);
            string whereSql = string.Empty;
            if (wheres.Count > 0)
            {
                whereSql = $"WHERE {string.Join(" AND ", wheres)}";
            }
            string sql = $"{SQL_QUERY_VIEW} {whereSql} ORDER BY A.MoveHouseConfigId ASC ";
            return await this.database.Slave.QueryAsync<MoveHouseConfigEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<MoveHouseConfigEntity>, int)> Query(long? parentId, bool? isEnable, int? level,
            int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (isEnable.HasValue)
            {
                wheres.Add("A.Enabled = ?Enabled");
                parameters.Add("?Enabled", isEnable.Value, DbType.Boolean);
            }
            if (parentId.HasValue)
            {
                wheres.Add("A.ParentId = ?ParentId");
                parameters.Add("?ParentId", parentId.Value, DbType.Int64);
            }
            if (level.HasValue)
            {
                wheres.Add("A.Level = ?Level");
                parameters.Add("?Level", level.Value, DbType.Int64);
            }
            string whereSql = string.Empty;
            if (wheres.Count > 0)
            {
                whereSql = $"WHERE {string.Join(" AND ", wheres)}";
            }
            string sqlCount = " SELECT COUNT(A.MoveHouseConfigId) FROM movehouseconfig A ";
            string sql = $@"{SQL_QUERY_VIEW} {whereSql} ORDER BY A.MoveHouseConfigId ASC 
                            LIMIT {(pageIndex - 1) * pageSize}, {pageSize};{sqlCount}{whereSql} ";
            return await this.database.Slave.QueryPageableListAsync<MoveHouseConfigEntity>(sql, parameters);
        }

        public async Task<int> DeleteAsync(long moveHouseConfigId)
        {
            var sql = $" {SQL_DELETE} WHERE MoveHouseConfigId=?MoveHouseConfigId ";
            return await this.database.Master.ExecuteAsync(sql, new { moveHouseConfigId }, database.Transaction);
        }

        public async Task<IEnumerable<MoveHouseConfigEntity>> GetAllSecondList(bool? isEnable)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (isEnable.HasValue)
            {
                wheres.Add("A.Enabled = ?Enabled");
                parameters.Add("?Enabled", isEnable.Value, DbType.Boolean);
            }
            wheres.Add("A.Level = 1");
            string whereSql = string.Empty;
            if (wheres.Count > 0)
            {
                whereSql = $"WHERE {string.Join(" AND ", wheres)}";
            }
            string sql = $"{SQL_QUERY_VIEW} {whereSql} ORDER BY A.MoveHouseConfigId ASC  ";
            return await this.database.Slave.QueryAsync<MoveHouseConfigEntity>(sql, parameters);
        }

        public async Task<IEnumerable<MoveHouseConfigEntity>> GetList(long moveHouseConfigId, bool? isEnable, int level)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (isEnable.HasValue)
            {
                wheres.Add("A.Enabled = ?Enabled");
                parameters.Add("?Enabled", isEnable.Value, DbType.Boolean);
            }
            if (level == 0)
            {
                wheres.Add("A.MoveHouseConfigId=?MoveHouseConfigId");
                parameters.Add("?MoveHouseConfigId", moveHouseConfigId, DbType.Int64);
            }
            else
            {
                wheres.Add("A.ParentId=?MoveHouseConfigId");
                parameters.Add("?MoveHouseConfigId", moveHouseConfigId, DbType.Int64);
            }
            wheres.Add("A.Level = ?Level");
            parameters.Add("?Level", level, DbType.Int64);
            string whereSql = string.Empty;
            if (wheres.Count > 0)
            {
                whereSql = $"WHERE {string.Join(" AND ", wheres)}";
            }
            string sql = $"{SQL_QUERY_VIEW} {whereSql} ORDER BY A.MoveHouseConfigId ASC ";
            return await this.database.Slave.QueryAsync<MoveHouseConfigEntity>(sql, parameters);
        }

        public async Task<IEnumerable<MoveHouseConfigEntity>> GetAllClientList(long moveHouseConfigId)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            string whereSql = string.Empty;
            wheres.Add("A.ParentId=?MoveHouseConfigId");
            parameters.Add("?MoveHouseConfigId", moveHouseConfigId, DbType.Int64);
            if (wheres.Count > 0)
            {
                whereSql = $"WHERE {string.Join(" AND ", wheres)}";
            }
            string sql = $"{SQL_QUERY_VIEW} {whereSql} ORDER BY A.MoveHouseConfigId ASC ";
            return await this.database.Slave.QueryAsync<MoveHouseConfigEntity>(sql, parameters);
        }

        public async Task<MoveHouseConfigEntity> GetParentSingle(long parentId)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            string whereSql = string.Empty;
            wheres.Add("A.MoveHouseConfigId=?MoveHouseConfigId");
            parameters.Add("?MoveHouseConfigId", parentId, DbType.Int64);
            if (wheres.Count > 0)
            {
                whereSql = $"WHERE {string.Join(" AND ", wheres)}";
            }
            string sql = $"{SQL_QUERY_VIEW} {whereSql} ORDER BY A.MoveHouseConfigId ASC ";
            return await this.database.Slave.QueryFirstOrDefaultAsync<MoveHouseConfigEntity>(sql, parameters);
        }

        public Task<decimal> GetMinPrice(long moveHouseConfigId)
        {
            throw new NotImplementedException();
        }
    }
}
